// // ignore_for_file: cast_nullable_to_non_nullable, unnecessary_nullable_for_final_variable_declarations, strict_raw_type
//
// import 'dart:async';
// import 'dart:io';
// import 'dart:typed_data';
//
// import 'dart:ui' as ui;
// import 'package:flutter/material.dart';
// import 'package:flutter/rendering.dart';
// import 'package:flutter_image_compress/flutter_image_compress.dart';
// import 'package:hixing_app/utils/logs.dart';
// import 'package:hixing_app/widgets/toast/toast.dart';
// import 'package:image_gallery_saver/image_gallery_saver.dart';
// import 'package:mime/mime.dart';
// import 'package:path_provider/path_provider.dart';
//
// /// image utils 图片处理
// class ImageUtils {
//   void mimeType() {
//     //'image/jpeg
//     final mimeType = lookupMimeType(
//       'https://test-saasstore.oss-cn-shenzhen.aliyuncs.com/10110010000042/card/image1670210172089.png',
//       headerBytes: [0xFF, 0xD8],
//     );
//     logs('mimeType  $mimeType');
//   }
//
//   /// 根据字节列表生成压缩图片
//   static Future<File> genAndCompressImageOfBytes(
//     Uint8List bytes, {
//     int quality = 50,
//     bool save = false,
//   }) async {
//     final Uint8List result =
//         await FlutterImageCompress.compressWithList(bytes, quality: quality);
//     final String tempDir = (await getTemporaryDirectory()).path;
//     final String filename =
//         'singular_${DateTime.now().millisecondsSinceEpoch}.jpeg';
//
//     return File('$tempDir/$filename')..writeAsBytesSync(result);
//   }
//
//   /// 图片压缩
//   static Future<File> imageCompressAndGetFile(
//     File file, {
//     int startLength = 2 * 1024 * 1024,
//   }) async {
//     if (file.lengthSync() < startLength) {
//       return file;
//     }
//     int quality = 100;
//     if (file.lengthSync() > startLength * 20) {
//       quality = 50;
//     } else if (file.lengthSync() > startLength * 10) {
//       quality = 60;
//     } else if (file.lengthSync() > startLength * 5) {
//       quality = 70;
//     } else if (file.lengthSync() > startLength * 2.5) {
//       quality = 80;
//     } else if (file.lengthSync() > startLength * 1.25) {
//       quality = 90;
//     }
//     final Directory dir = await getTemporaryDirectory();
//     final String targetPath =
//         '${dir.absolute.path}/${DateTime.now().millisecondsSinceEpoch}.jpg';
//     final File? result = await FlutterImageCompress.compressAndGetFile(
//       file.absolute.path,
//       targetPath,
//       quality: quality,
//     );
//
//     return result!;
//   }
//
//   /// 截屏保存图片
//   static Future<dynamic> saveScreen(GlobalKey rootKey,
//       {bool isUtf8 = false}) async {
//     final RenderRepaintBoundary? boundary =
//         rootKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
//     if (boundary == null) {
//       return;
//     }
//     final ui.Image image = await boundary.toImage(pixelRatio: 3);
//     final ByteData? byteData =
//         await image.toByteData(format: ui.ImageByteFormat.png);
//     // final ByteData byteData = await (image.toByteData(
//     //     format: ui.ImageByteFormat.png) as FutureOr<ByteData>);
//     if (isUtf8) {
//       return byteData?.buffer.asUint8List();
//     } else {
//       if (byteData != null) {
//         final FutureOr result = await ImageGallerySaver.saveImage(
//             byteData.buffer.asUint8List(),
//             quality: 100);
//         if (result != null) {
//           AppToast.showSuccess('已保存到图片相册');
//         }
//       }
//     }
//   }
//
//   /// 截屏 返回file
//   static Future<File?> screenShot(GlobalKey rootKey) async {
//     final RenderRepaintBoundary? boundary =
//         rootKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
//     if (boundary == null) {
//       return null;
//     }
//     final ui.Image image = await boundary.toImage(pixelRatio: 3);
//     final ByteData byteData = await (image.toByteData(
//         format: ui.ImageByteFormat.png) as FutureOr<ByteData>);
//     final path = '${(await getTemporaryDirectory()).path}/ScreenShot_';
//     //生成file文件格式
//     File file = File('$path${DateTime.now().millisecondsSinceEpoch}.jpg');
//     file = await file.writeAsBytes(byteData.buffer.asUint8List(), flush: true);
//     return file;
//   }
//
//   ///  ----------------------------------------------------------------------------------------------
//   ///  ----------------------------------------------------------------------------------------------
//   ///  UiImageUtil    ui.Image 相关操作 返回 ui.Image 对象
//   ///  ----------------------------------------------------------------------------------------------
//   ///  ----------------------------------------------------------------------------------------------
//
//   /// get ui.Image by Memory
//   static Future<ui.Image> loadImageMemory(Uint8List bytes) async {
//     ImageStream stream;
//     // ByteData data = await rootBundle.load(path);
//     // stream = Image.memory(data.buffer.asUint8List()).image .resolve(ImageConfiguration.empty);
//     stream = Image.memory(bytes).image.resolve(ImageConfiguration.empty);
//     final Completer<ui.Image> completer = Completer<ui.Image>();
//     void listener(ImageInfo frame, _) {
//       final ui.Image image = frame.image;
//       completer.complete(image);
//       stream.removeListener(ImageStreamListener(listener,
//           onError: (Object o, StackTrace? s) => _loadFailed));
//     }
//
//     stream.addListener(ImageStreamListener(listener,
//         onError: (Object o, StackTrace? s) => _loadFailed));
//     return completer.future;
//   }
//
//   /// get ui.Image by File
//   static Future<ui.Image> loadImageFile(File file) async {
//     ImageStream stream;
//     stream = Image.file(file).image.resolve(ImageConfiguration.empty);
//     final Completer<ui.Image> completer = Completer<ui.Image>();
//     void listener(ImageInfo frame, _) {
//       final ui.Image image = frame.image;
//       completer.complete(image);
//       stream.removeListener(ImageStreamListener(listener,
//           onError: (Object o, StackTrace? s) => _loadFailed));
//     }
//
//     stream.addListener(ImageStreamListener(listener,
//         onError: (Object o, StackTrace? s) => _loadFailed));
//     return completer.future;
//   }
//
//   /// get ui.Image by Asset
//   static Future<ui.Image> loadImageAsset(String asset) async {
//     ImageStream stream;
//     stream = Image.asset(asset).image.resolve(ImageConfiguration.empty);
//     final Completer<ui.Image> completer = Completer<ui.Image>();
//     void listener(ImageInfo frame, _) {
//       final ui.Image image = frame.image;
//       completer.complete(image);
//       stream.removeListener(ImageStreamListener(listener,
//           onError: (Object o, StackTrace? s) => _loadFailed));
//     }
//
//     stream.addListener(ImageStreamListener(listener,
//         onError: (Object o, StackTrace? s) => _loadFailed));
//     return completer.future;
//   }
//
//   /// get ui.Image by NetWork
//   static Future<ui.Image> loadImageNetWork(String url) async {
//     ImageStream stream;
//     stream = Image.network(url).image.resolve(ImageConfiguration.empty);
//     final Completer<ui.Image> completer = Completer<ui.Image>();
//     void listener(ImageInfo frame, _) {
//       final ui.Image image = frame.image;
//       completer.complete(image);
//       stream.removeListener(ImageStreamListener(listener,
//           onError: (Object o, StackTrace? s) => _loadFailed));
//     }
//
//     stream.addListener(ImageStreamListener(listener,
//         onError: (Object o, StackTrace? s) => _loadFailed));
//     return completer.future;
//   }
//
//   static void _loadFailed(dynamic exception, StackTrace stackTrace) {
//     debugPrint(' _loadFailed  \n $exception');
//   }
// }
